﻿using System;
using System.Collections.Generic;
using AdaptiveAgents.Agents;

namespace AdaptiveAgents.TeammateModels
{
    class AdaptiveNoRaiseTeammateModel : AdaptiveTeammateModel_old
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="me">My agent</param>
        /// <param name="agent">The agent I'm modeling</param>
        /// <param name="env">The environment</param>
        public AdaptiveNoRaiseTeammateModel(Agent me, Agent agent, Environment env)
            : base(me, agent, env)
        {        }

        /// <summary>
        /// A tested methodology for doing the modeling of the other agent
        /// Now we say that if an agent has passed to a different agent the before - this is exploration.
        /// The epsilon value of the agent is set to the percentage that the agent did exploration from all his plays
        /// The competence value of the agent is set to be the average of all the values of the agent
        /// </summary>
        /// <param name="observations"></param>
        /// <returns></returns>
        public override double[] estimateEpsilonAndCometence(List<Observation> observations)
        {          
            double[] result = new double[2]; //the result array
            
            int epsCounter = 0; //exploration counter
            
            int lastPass = -1; //number of agent that the agent passed last time
            int compCounter = 0; 
            double compSum = 0;

            foreach (Observation obs in observations)
            {
                if (obs.ID == this.agentID)
                {
                    //for competence
                    compCounter += 1;
                    compSum += obs.Value;

                    //for epsilon
                    if (lastPass == -1) //first time only
                        lastPass = obs.NextAgent;
                    else if (obs.NextAgent != lastPass) // (exploration)
                        ++epsCounter;

                    lastPass = obs.NextAgent;
                }
            }

            result[0] = (epsCounter * 1.0) / compCounter;
            result[1] = compSum / compCounter;

            return result;
        }
    }
}
